/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset.bookmark;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import cn.edu.nju.ws.sview.cache.UserNameIDCache;
import cn.edu.nju.ws.sview.database.DBConnectionFactory;
import cn.edu.nju.ws.sview.wyge.relations.datasets.DatasetItem;
import cn.edu.nju.ws.sview.wyge.relations.index.SearchIndexManager;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-23
 * 
 */
@Deprecated
public class Bookmark_copy {
	/**
	 * 是否已经存在Bookmark。bookmarkName以及userName唯一确定一个bookmark.
	 * 
	 * @param bookmarkName
	 * @param userName
	 * @return
	 * @throws Throwable
	 */
	public static boolean isExist(String bookmarkName, String userName)
			throws Throwable {
		int dsid = getBookmarkID(bookmarkName, userName);
		if (dsid != -1) {
			return true;
		}
		return false;
	}

	public static int getBookmarkID(String bookmarkName, String userName)
			throws Throwable {
		int dsid = -1;
		Connection con = DBConnectionFactory.getConnection();
		PreparedStatement selps = con
				.prepareStatement("SELECT id FROM custom_dataset where name = ? and user_id = ?");
		int uid = UserNameIDCache.getInstance().getUserID(userName);
		selps.setString(1, bookmarkName);
		selps.setInt(2, uid);
		ResultSet selrs = selps.executeQuery();
		if (selrs.next()) {
			dsid = selrs.getInt(1);
		}
		selrs.close();
		selps.close();
		con.close();
		return dsid;
	}

	/**
	 * 插入Bookmark
	 * 
	 * @param bookmarkName
	 *            新的CustomDataset名称
	 * @param userName
	 *            用户名称
	 * @param ds_type
	 *            要插入的dataset_type
	 * @param ds_id
	 *            要插入的dataset_id
	 * @param lang
	 *            当前的语言
	 * @throws Throwable
	 */
	public static void insert(String bookmarkName, String userName,
			String ds_type, int ds_id, String lang) throws Throwable {
		Connection con = DBConnectionFactory.getConnection();
		PreparedStatement insertCustomDatasetPS = con
				.prepareStatement("insert into custom_dataset (name, user_id, time, lang) values(?,?,now(),?)");
		PreparedStatement insertCDDocsPS = con
				.prepareStatement("insert ignore into custom_dataset_doc (dataset_id, doc_id) values (?, ?)");
		PreparedStatement getCDDocsPS = con
				.prepareStatement("SELECT doc_id FROM custom_dataset_doc where dataset_id = ?");
		// custom dataset以及他的documents插入数据库

		boolean isNew = false;
		int new_ds_id = getBookmarkID(bookmarkName, userName);
		if (new_ds_id == -1) {
			insertCustomDatasetPS.setString(1, bookmarkName);
			insertCustomDatasetPS.setInt(2, UserNameIDCache.getInstance()
					.getUserID(userName));
			insertCustomDatasetPS.setString(3, lang);
			insertCustomDatasetPS.executeUpdate();
			isNew = true;
			new_ds_id = getBookmarkID(bookmarkName, userName);
		}

		if (ds_type.equals("custom")) {
			getCDDocsPS.setInt(1, ds_id);
			ResultSet getCDDocsRS = getCDDocsPS.executeQuery();
			int j = 0;
			while (getCDDocsRS.next()) {
				int doc_id = getCDDocsRS.getInt(1);
				insertCDDocsPS.setInt(1, new_ds_id);
				insertCDDocsPS.setInt(2, doc_id);
				insertCDDocsPS.addBatch();
				j++;
				if (j % 200 == 0) {
					insertCDDocsPS.executeBatch();
					insertCDDocsPS.clearBatch();
				}
			}
			insertCDDocsPS.executeBatch();
			insertCDDocsPS.clearBatch();
			getCDDocsRS.close();
		} else {
			insertCDDocsPS.setInt(1, new_ds_id);
			insertCDDocsPS.setInt(2, ds_id);
			insertCDDocsPS.executeUpdate();
		}

		// add index for new custom dataset
		if (isNew == true) {
			ArrayList<DatasetItem> dss = new ArrayList<DatasetItem>();
//			dss.add(new DatasetItem("custom", new_ds_id, bookmarkName));
			DatasetItem di = new DatasetItem("custom", new_ds_id, null);
			di.setShowName(bookmarkName);
			dss.add(di);
			
//			IndexManager.getIndexManager().addDatasets(dss);
			SearchIndexManager.getIndexManager().addDatasets(dss);
		}

		insertCustomDatasetPS.close();
		insertCDDocsPS.close();
		getCDDocsPS.close();
		con.close();
	}
}
